package com.cngphone.AnFetion;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cngphone.AnFetion.MessageXml.AddBuddyApplicationMessage;
import com.cngphone.AnFetion.tools.LogUtil;
import com.cngphone.AnFetion.tools.StrUtil;
import com.cngphone.AnFetion2.R;

public class HandleContactRequest extends Activity implements OnClickListener {
    private FetionService mBoundService;
    private String nickname = null;
    private boolean accept = false;
    protected AddBuddyApplicationMessage cm;
    private static final String TAG = "HandleContactRequest";
    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((FetionService.LocalBinder) service).getService();

            String uri = cm.uri;
            if (Settings.DEBUG) LogUtil.d(TAG, uri);
            if (mBoundService.isConnected()) {
                ArrayList<FetionFriend> buddies = mBoundService.getFriendManager().getBuddies();
                FetionFriend ff = mBoundService.getFriendManager().getFriend(uri);
                if (accept) {
                    if (ff == null) {
                        ff = new FetionFriend();
                        ff.Uri = uri;
                        ff.type = Settings.TYPE_FRIEND;
                        ff.isMe = false;
                        buddies.add(ff);
                    }
                }
                boolean success = mBoundService.sendAccept(new FetionUri(uri), accept, nickname);
                if (Settings.DEBUG) LogUtil.d(TAG, "result: " + success);
                if (success && accept) {
                    Toast.makeText(HandleContactRequest.this, "您已同意对方请求，现在您可以与他发送和接收消息了", Toast.LENGTH_SHORT)
                            .show();
                    // mBoundService.getFriendsAndReserveNotify();
                    mBoundService.getFriendInfo(ff);
                    MiscUtil.notifyFriendChange(HandleContactRequest.this);
                } else if (!success) {
                    if (accept) mBoundService.getFriendManager().getBuddies().remove(ff);
                    Toast.makeText(HandleContactRequest.this, "处理不成功, 请检查是否已经确认过此申请!", Toast.LENGTH_SHORT)
                            .show();
                }
            }

            // 取消绑定服务并关闭
            unbindService(mConnection);
            finish();

        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        int id = bundle.getInt("notify_id", -1);
        if (Settings.DEBUG) LogUtil.d(TAG, "notify: " + id);
        if (id > 0) {
            MiscUtil.cancelNotification(this, id);
        }
        cm = (AddBuddyApplicationMessage) bundle.getSerializable("contact");
//        ArrayList<FetionFriend> buddies = mBoundService.getReserveNotify().getBuddies();
//        FetionFriend ff = MiscUtil.getFriend(cm.uri, buddies);
//        if (ff != null && ff.type == Settings.TYPE_FRIEND) {
//            this.finish();
//            return;
//        }
        setContentView(R.layout.confirm);
        ((TextView) findViewById(R.id.message_invite)).setText(bundle.getString("message") + "\n发送日期: "
                + StrUtil.realTime(cm.time));
        ((EditText) findViewById(R.id.nickname)).setText(cm.desc);
        findViewById(R.id.agree).setOnClickListener(this);
        findViewById(R.id.disagree).setOnClickListener(this);
        findViewById(R.id.cancel).setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.cancel) {
            finish();
            return;
        }
        if (v.getId() == R.id.agree) {
            accept = true;
            nickname = ((EditText) findViewById(R.id.nickname)).getText().toString();
        } else {
            accept = false;
        }
        // 绑定服务
        this.bindService(MiscUtil.getFetionServiceIntent(this), mConnection, Context.BIND_AUTO_CREATE);
    }
}
